<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode" >
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue" @changeCheckBox="changeCheckBox"></om-autoform>
	</om-addpage>
</template>

<script>
	import {errorModal,ignoreHtml,getUrl,confirmModal, ptFloat, minusF } from '@/static/js/common.js';
	import {buildFormData,formValidation, tranferToEditors, changeEditor,setSubmitSubValue, configSelectData, hasValue,formShowLoading ,getFormFields,formModelLoad,getWorkCacheKey} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	
	export default{
		data(){
			return buildFormData({
				userCacheKey:"hr-kq-repair-user-cache",
				timeInfoInfoList:[]
			})
		},
		provide(){
			return{
				formPage:this
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			getUserSalaryPosInfo(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getUserSalaryPosInfo",
					{
						userId:id
					}
				).then(res=>{
					if(res.succeed){
						_that.$refs.autoform.formdata.salarycatId = res.value.salaryCatId
						_that.$refs.autoform.formdata.salarycatName = res.value.salaryCatName
						_that.$refs.autoform.formdata.parentUserName  =  res.value.parentUserName
					}
				})
			},
			changeCheckBox(e){
				let _that = this
				let timeArr = []
				if(hasValue(e.detail)){
					if(Object.prototype.toString.call(e.detail.value)==='[object Array]'){
						if(e.detail.value.indexOf('5987') > -1){
							 timeArr = _that._.cloneDeep(_that.timeInfoInfoList)
							_that.subEditorObj["timeInfo"].workList = timeArr
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr1',JSON.stringify(timeArr))
							_that.$set(_that.formdata,'objStr1',JSON.stringify(timeArr))
						}else{
							_that.subEditorObj["timeInfo"].workList= ''
						}
					}
				}else{
					if(e.indexOf('5987') > -1 || e[0].indexOf('5987') > -1){
						 timeArr = _that._.cloneDeep(_that.timeInfoInfoList)
						_that.subEditorObj["timeInfo"].workList = timeArr
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr1',JSON.stringify(timeArr))
						_that.$set(_that.formdata,'objStr1',JSON.stringify(timeArr))
					}else{
						_that.subEditorObj["timeInfo"].workList= ''
					}
				}
			},
			onSelectTime(){
				var _that = this
				this.verifyDateFunc()
			},
			verifyDateFunc(){
				let _that = this
				let obj = {
					 date:_that.$refs.autoform.formdata.repairTime
				};
				_that.om.request(
				'wxRepairAttence/verifyDate',
				 obj,
				 'get'
				).then(res =>{
					if(res.succeed){
						_that.loadAttendNodes();
					}else{
						_that.$refs.autoform.clear('repairInfo','repairTime')
						_that.om.toast(res.msg,4000)
					}
				})
			},
			getMsg(time){
				let _that = this
				this.om.request(
					'repairAttence/checkTime',
					{
						time:time,
						infoId:this.userId
					}
				).then(res =>{
					if(res.value.success){
						errorModal(_that,'提示信息',ignoreHtml(res.value.msg),function(){
						})
					}
				})
			},
			getReasonList(){
				this.om.request(
					'tsysdict/getCombobox',
					{
						code:'38037'
					}
				).then(res=>{
					this.$refs.autoform.changeEditor('repairInfo','reasonId','data',configSelectData(res,'id','dicname'))
					this.$refs.autoform.initSelectIndex('repairInfo','reasonId')
				})
			},
			loadAttendNodes(){
				let _that = this
				let obj ={
					date:_that.$refs.autoform.formdata.repairTime
				}
				_that.om.request(
				'wxRepairAttence/getAttendList',
				obj,
				'get'
				).then(res =>{
					if(res.succeed){
						if(!res.value.attendNodeList || !res.value.attendNodeList.length){
							_that.om.toast(_that.$refs.autoform.formdata.repairTime + '不需要补签')
							_that.$refs.autoform.clear('repairInfo','repairTime')
							}else{
							//保存补签人姓名
							if(!_that.edit){
								_that.getMsg(_that.$refs.autoform.formdata.repairTime)
							}
							let infoName = _that.$refs.autoform.formdata.infoName
							let infoId = _that.$refs.autoform.formdata.infoID
							if(_that.timeInfoInfoList.length<1){
								_that.timeInfoInfoList = _that._.cloneDeep(res.value.addTimeList)
							}
							_that.changeCheckBox([_that.$refs.autoform.formdata.attendNodeId])
							_that.$refs.autoform.attendNodeList = res.value.attendNodeList
							_that.$refs.autoform.overtimeList = res.value.addTimeList
							_that.$refs.autoform.changeEditor('repairInfo','attendNodeId','data',configSelectData(res.value.attendNodeList,'id','text',true))
							if(res.value.attendNodeList.length ==1 && res.value.attendNodeList[0].attributes.dictZeroCode === "37987"){
								let id = ""
								for(let item of res.value.attendNodeList){
										_that.$refs.autoform.$set(item,"checked",true)
										id = item.id+''
								}
								 _that.changeCheckBox([id])
							}
							if(_that.edit){
								for(let item of res.value.attendNodeList){
									if(_that.$refs.autoform.formdata.attendNodeId.indexOf(item.id) != -1){
										_that.$refs.autoform.$set(item,"checked",true)
									}
								}
								 _that.changeCheckBox([_that.$refs.autoform.formdata.attendNodeId])
							}
							_that.$refs.autoform.changeEditor('instPosInfo', "infoId", {
								defaultValue:infoId,
								defaultName:infoName,
							},null)
						}
					}else{
						_that.om.toast(res.msg)
					}
				})
			},
			initLoad(options){
				let _that = this;
				var ajaxArr= []
				var fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"repairAttence/getInfo",
						param:{
							id:fileId
						}
					})
				}
				ajaxArr.push(urlUtil.sysdictComboboxUrl("38037"))
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor('reasonInfo','reason','required',true)
				if(_that.subEditorObj['timeInfo']){
					changeEditor("timeInfo", "objStr1", {
						showAdd:false,
						showDel:false,
						titleTemplate: "  <%='开始时间:'+ applyStartTime  + '结束时间:'+ applyEndTime || '' %>",
						subtitle1Template: "实际开始时间:<%= actualStartTime || '未填写' %> 实际结束时间:<%= actualEndTime || '未填写' %>",
					}, null, mainData)
				}	
				changeEditor('instPosInfo', "infoId", {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield:urlUtil.findUserAsSameInst().namefield,
						extrafield:urlUtil.findUserAsSameInst().extrafield,
						searchfield:"q",
						initload:true,
						url:urlUtil.findUserAsSameInst().url ,
						cachename:_that.userCacheKey,
						placeholder:"请输入姓名"
					}
				},null,mainData);
				changeEditor('repairInfo','reasonId','data',configSelectData(res[0],'id','dicname'),mainData)
				changeEditor('topinfo','zuserId',{
					defaultName:_that.userName,
					defaultValue:_that.userId
				},null, mainData);
				changeEditor('fileInfo','formUpload','fileUploadKey',"32",mainData)
				if(res[0][0]){
					changeEditor('repairInfo','reasonId',{
						defaultName:res[0][0].dicname,
						defaultValue:res[0][0].id
					},null,mainData)
				}
				// changeEditor('instPosInfo','infoId',{
				// 	defaultValue:_that.userId,
				// 	defaultName:_that.userName,
				// },null,mainData)
				if(fileId){
					var fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					for(var i in  res[0].value){
						if(fieldArr.includes(i)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,i,res[0].value[i])
							_that.$set(_that.formdata,i,res[0].value[i])
						}
					}
					_that.timeInfoInfoList = res[0].value.times
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"formUpload",res[0].value.fileList)
					_that.formdata.formUpload = res[0].value.fileList
					
					changeEditor('fileInfo','formUpload',"fileUploadKey","32",mainData)
					if(_that.formdata.formUpload){
						var tempFileList = []
						var fileUrlList = _that.formdata.formUpload.map(t=>{
							tempFileList.push(t.fileSaveName)
							return t.fileSaveName
						})
						changeEditor("fileInfo","formUpload",'fileUrlList',fileUrlList,mainData)
						changeEditor("fileInfo","formUpload","tempFileList",tempFileList,mainData)
					}
					_that.$nextTick(function(){
						this.loadAttendNodes()
						this.getReasonList()
					})
				}
				},options)
			},
			formSubmit(){
				let _that = this
				if(_that.subEditorObj['timeInfo'].workList.length){
					   for(let item of _that.timeInfoInfoList){
					   	if(!item.actualStartTime || !item.actualEndTime){
					   		_that.om.toast('加班补签信息中“实际开始时间”和“实际结束时间”不能为空，请继续编辑',3000)
					   		return
					   	}
					}   
				}
				_that.formdata.newFileFlag = true
				if(_that.edit){
					_that.formdata.newFileFlag = false
						if(_that.formdata.formUpload.length > 1){
							for(let index in _that.formdata.formUpload){
									if(!_that.formdata.formUpload[index].hasOwnProperty('id')){
										_that.formdata.newFileFlag = true
									}
								}
						}else if(_that.formdata.formUpload.length <= 1){
							_that.formdata.newFileFlag = true
						}
					_that.formdata.fileListStr = JSON.stringify(_that.formdata.formUpload)
					_that.formdata.formUpload = JSON.stringify(_that.formdata.formUpload)
					_that.om.formSave('wxRepairAttence/save',_that.formdata,_that)
				}else{
					_that.om.formSave('wxRepairAttence/save',_that.formdata,_that)
				}
				
			}
		},
		onLoad(options){
			this.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				let editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						_that.timeInfoInfoList = res.data
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			uni.getStorage({
				key: _that.userCacheKey,
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.infoId = d.id
						formdata.infoName = d.username
						formdata.salaryPosId = d.salaryPosId
						formdata.salaryPosName = d.salaryPosName
						formdata.instId = d.salaryPosInstId
						formdata.instName = d.salaryPosInstName
						_that.getUserSalaryPosInfo(d.id)
						formdata.parentInstName = d.parentInstName
						formdata.parentPlaceId = formdata.parentPlaceId
						uni.removeStorage({
							key: _that.userCacheKey
						});
					}
				}
			});
		}
	}
	}
</script>

<style>
</style>